package com.liuyi.netty.im.lychat.modular.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.liuyi.netty.im.commons.codes.data.event.Session;
import com.liuyi.netty.im.lychat.config.base.BaseController;
import com.liuyi.netty.im.lychat.modular.entity.ChatSession;
import com.liuyi.netty.im.lychat.modular.service.IChatSessionService;
import com.liuyi.rapid.develop.tool.resp.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.liuyi.rapid.develop.tool.resp.ResponseCall.success;
import static com.liuyi.rapid.develop.tool.resp.ResponseCall.successMsg;
import static org.springframework.http.ResponseEntity.ok;

/**
 * <p>
 * 聊天会话表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-12-29
 */
@Api(value = "", tags = {"聊天会话表"})
@RestController
@RequestMapping("/modular/chat-session")
public class ChatSessionController implements BaseController<ChatSession, IChatSessionService> {

    @Resource
    private IChatSessionService chatSessionService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "sessionId", value = "会话id")
    })
    @ApiOperation(value = "获取会话", notes = "会重置未读会话", httpMethod = "GET")
    @GetMapping("/get/{sessionId}")
    public ResponseEntity<Response<Session>> get(@PathVariable String sessionId) {
        String userId = StpUtil.getLoginIdAsString();
        server().cache().resetUnreadCount(userId, sessionId);
        ChatSession chatSession = chatSessionService.cache().getByUserIdAndSessionId(userId, sessionId);
        if (chatSession == null) {
            chatSession = server().createSession(userId, sessionId);
        }
        return ok(success(server().wrapperSession(chatSession)));
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "string", name = "sessionId", value = "")
    })
    @ApiOperation(value = "删除会话", notes = "", httpMethod = "POST")
    @PostMapping("/del/{sessionId}")
    public ResponseEntity<Response<Void>> del(@PathVariable String sessionId) {
        server().removeSession(StpUtil.getLoginIdAsString(), sessionId);
        return ok(successMsg("删除成功"));
    }

//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "path", dataType = "string", name = "sessionId", value = "会话id")
//    })
//    @ApiOperation(value = "消息已读", notes = "", httpMethod = "POST")
//    @PostMapping("/readCount/{sessionId}")
//    public ResponseEntity<Response<Session>> readCount(@PathVariable String sessionId) {
//        String userId = StpUtil.getLoginIdAsString();
//        server().cache().resetUnreadCount(userId, sessionId);
//        ChatSession session = server().cache().getByUserIdAndSessionId(userId, sessionId);
//        return ok(success(server().wrapperSession(session)));
//    }

    @Override
    public IChatSessionService server() {
        return chatSessionService;
    }
}

